<?php
/*
 * 来路分析相关
 * copyright:living10
 */
class SystemFunctions_model extends CI_Model{
 	public function __construct()
 	{
 		$this->load->database();
 		$this->load->library('session');
 		$this->load->model('anti_model');
 	}
	//来路分析
	/*
	 * 修改登录密码
	 */
	 public function showKeyWord()
	 {
	 	//

		$sql = "select searcher,keyword,price,id from tj_keyword where wid=".$this->session->userdata('wid');
		$query = $this->db->query($sql);
		$result = array();
		$result['baidu']['count']=0;
		$result['google']['count']=0;
		$result['sogou']['count']=0;
		foreach($query->result_array() as $k=>$val){
			$result[$val['searcher']]['data'][] =  $val;
			@$result[$val['searcher']]['searcher_ids'] .= ','.$val['id'];
			@$result[$val['searcher']]['count'] +=1;
		}

		return $result;
	 }

	 /*
	  * 保存关键词
	  */
	 public function saveKeyWord()
	 {
	 	/*百度处理*/
	 	$bak_baidu_id = $this->input->post('bak_baidu');
	 	$baidu_keywords = $this->input->post('baidu_keywords');
	 	$baidu_key_price = $this->input->post('baidu_key_money');
		$this->keyword_fun($bak_baidu_id,$baidu_keywords,$baidu_key_price,'baidu');
	 	/*谷歌处理*/
	 	$bak_google_id = $this->input->post('bak_google');
	 	$google_keywords = $this->input->post('google_keywords');
	 	$google_key_price = $this->input->post('google_key_money');
		$this->keyword_fun($bak_google_id,$google_keywords,$google_key_price,'google');
		/*搜狗处理*/
	 	$bak_sogou_id = $this->input->post('bak_sogou');
	 	$sogou_keywords = $this->input->post('sogou_keywords');
	 	$sogou_key_price = $this->input->post('sogou_key_money');
		$this->keyword_fun($bak_sogou_id,$sogou_keywords,$sogou_key_price,'sogou');
		$this->anti_model->setWebKeyPriceData($this->session->userdata('wid'));
		return true;
	 }

	 /*
	  * ip黑白名单设置
	  */
	  public function ipblacklist()
	  {
		$sql = "select * from tj_blackip where wid=".$this->session->userdata('wid');
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		$result = array();
		foreach($arr as $k=>$val){
			$val['IPStart'] =  long2ip($val['IPStart']) ;
			$val['IPEnd'] =  long2ip($val['IPEnd']) ;
			$result[] = $val;
		}

		return $result;
	  }

	  /*
	   * 保存ip黑名单
	   */
	  public function save_ipblack()
	  {
	  	$data['IPStart'] = ip2long($this->input->post("IPStart"));
	  	$data['IPEnd'] = ip2long($this->input->post("IPEnd"));
		$data['note'] = $this->input->post("note");
		$data['wid'] = $this->session->userdata('wid');
		$data['InsTime'] = date('Y-m-d H:i:s');
		$sql = $this->db->insert_string('tj_blackip', $data);


		$this->db->query($sql);
		return true;
	  }

	  /*
	   * 获取恶意点击设置信息
	   */
	   public function get_MaliciousClicksSetting()
	   {
	   	$sql = "select * from tj_setting where wid=".$this->session->userdata("wid");
	   	$q=$this->db->query($sql);
	   	$arr = $q->row_array();
		return $arr;

	   }

	   /*
	    * 更新恶意点击设置
	    */
	    public function upd_MaliciousClicksSetting()
	    {
		    $data = array();
			$data['lmtTime'] = intval($this->input->post("lmtTime"));
			$data['wuxiao_Radio'] = intval($this->input->post('wuxiao_Radio'));
			$data['eyi_Radio'] = intval($this->input->post('eyi_Radio'));
			$data['close_24'] = intval($this->input->post('close_24'));
			$data['close_hours'] = intval($this->input->post('close_hours'));
			$data['close_Times_Jinggao'] = intval($this->input->post('close_Times_Jinggao'));
			$data['close_Times_Zuzhi'] = intval($this->input->post('close_Times_Zuzhi'));
			$data['msgAlarm'] = htmlspecialchars($this->input->post('msgAlarm'));
			$this->db->update('tj_setting', $data,array('wid'=>$this->session->userdata('wid')));
			$this->anti_model->setWebSettingData($this->session->userdata('wid'));
			return true;
	    }

	    /*
	     * 更新意向度设置
	     */
	    public function upd_intent()
	    {
	    	$upd = array();
	    	$upd['PtlATime'] = $this->input->post('PtlATime');
	    	$upd['PtlAPageNum'] = $this->input->post('PtlAPageNum');
	    	$upd['PtlRadio'] = $this->input->post('PtlRadio');
	    	$upd['yxd_ey'] = $this->input->post('yxd_ey');
	    	$upd['yxd_ky'] = $this->input->post('yxd_ky');
	    	$upd['yxd_zc'] = $this->input->post('yxd_zc');
	    	$upd['yxd_qz'] = $this->input->post('yxd_qz');
	    	$upd['yxd_zy'] = $this->input->post('yxd_zy');
	    	$where = array('wid'=>$this->session->userdata("wid"));
	    	return $this->db->update("tj_setting",$upd,$where);
	    }
	    /*
	     * 删除ip黑白名单
	     */
	     public function del_ipblack(){
	     	$id = $this->input->get("id");
	     	$sql = "delete from tj_blackip where id=".$id;
	     	return $this->db->query($sql);
	     }
	     /*
	      * 处理关键词
	      */
		function keyword_fun($bak_baidu_id,$baidu_keywords,$baidu_key_price,$search_name='baidu')
		{
			$bak_baidu_id = explode(',',$bak_baidu_id);
		 	foreach($bak_baidu_id as $k=>$v)
		 	{
				$id = intval($v);
				if($id>0)
				{
					if(!empty($baidu_keywords[$k])){
						$data = array();
						$data['keyword'] = $baidu_keywords[$k];
						$data['price'] = floatval($baidu_key_price[$k]);
						$where = 'id='.$id;
						$sql = $this->db->update_string('tj_keyword', $data,$where);
						$this->db->query($sql);
					}else{
						$this->db->delete('tj_keyword',array('id'=>$id));
					}
				}else{
					if(!empty($baidu_keywords[$k])){
						$new_data = array();
						$new_data['keyword'] = $baidu_keywords[$k];
						$new_data['price'] = floatval($baidu_key_price[$k]);
						$new_data['Searcher']=$search_name;
						$new_data['wid'] = $this->session->userdata('wid');
						$sql = $this->db->insert_string('tj_keyword', $new_data);
						$this->db->query($sql);
					}
				}
		 	}
		}
	public function showVid($sday,$eday)
	{
		$int_sdate = strtotime($sday);
		$int_edate = strtotime($eday." +1 day");
		$result = array();
		$where = " where s.wid=".$this->session->userdata('wid')." and s.arriveTime>=".$int_sdate." and s.arriveTime<=".$int_edate." ";
		$sql = "select v.city,v.vid,v.state,sum(stopTime) as total_stopTimes,sum(isbad) as total_eyiclicks,count(s.arriveCount) as total_arriveCount " .
				"from tj_stat as s  left join tj_vistor as v on s.vid=v.vid  ".$where."  GROUP BY s.vid";
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		foreach($arr as $k=>$v){
			$v['avg_stopTimes'] = round($v['total_stopTimes']/$v['total_arriveCount'],2);
			$v['dianjibi'] = round(($v['total_arriveCount']-$v['total_eyiclicks'])*100/$v['total_arriveCount'],2);
			$result['viddata'][] = $v;
		}
		return $result;
	}

	public function editVid($vid,$state)
	{
		$this->db->update('tj_vistor',array('state'=>$state),array('vid'=>$vid));
		$vid_arr = $this->redisinit->get('vid_'.$vid);
		$vid_arr['state'] = $state;
		$this->redisinit->set('vid_'.$vid,$vid_arr);

		$wid = $this->session->userdata('wid');
		$vid_vid = $this->redisinit->get('wid_vid_'.$wid.'_'.$vid);
		$vid_vid['bad_count'] = 0;
		$vid_vid['eyi_state'] = 0;
		$this->redisinit->set('wid_vid_'.$wid.'_'.$vid,$vid_vid);
	}
}



















?>
